<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="showDialog =true"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="currentList">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button size="small" type="success" @click="assignPerm(scope.row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(scope.row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="deleteRole(scope.row.id)">删除</el-button>
                </template>

              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                layout="prev,pager,next"
                :total="page.total"
                :page-size="page.pagesize"
                @current-change="currentChange"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="formData.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="formData.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="formData.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="formData.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>

        </el-tabs>
      </el-card>

      <!--页面弹窗 -->
      <el-dialog :title="showTitle" :visible="showDialog" @close="btnCancel">
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnCancel">取消</el-button>
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>

      <!-- 分配权限管理弹窗 -->
      <el-dialog title="分配角色" :visible="showPermDialog" @close="btnPermCancel">
        <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
        <!-- 绑定权限树 -->
        <el-tree
          ref="permTree"
          :data="permList"
          :props="{label:'name'}"
          :default-expand-all="true"
          :show-checkbox="true"
          :check-strictly="true"
          node-key="id"
        />
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button type="primary" size="small" @click="btnPermOK">确定</el-button>
            <el-button size="small" @click="btnPermCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
// 获取公司信息
import { getCompanyInfo, getRoleList, delRole, getRoleDetail, updateRole, addRole, assignPerm } from '@/api/setting'
import { getPermissionList } from '@/api/permission'
import { listTotreeData } from '@/utils'
export default {
  data() {
    return {
      formData: {
        name: '',
        companyAddress: '',
        mailbox: '',
        remarks: ''
      },
      page: {
        page: 1,
        pagesize: 2,
        total: 0
      },
      showDialog: false,
      currentList: [],
      // 员工编辑数据信息
      roleForm: {},
      rules: {
        name: [
          { required: true, message: '数据不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '数据不能为空', trigger: 'blur' }
        ]
      },
      // 权限弹窗的数据
      showPermDialog: false,
      permList: [],
      roleId: ''
    }
  },
  computed: {
    ...mapGetters(['companyId']),
    showTitle() {
      return this.roleForm.id ? '编辑信息' : '新增员工'
    }
  },
  created() {
    // 调用获取公司信息
    this.getCompanyInfo()
    // 获取员工信息
    this.getRoleList()
    console.log(this.currentList)
  },
  methods: {
    // 获取公司信息
    async getCompanyInfo() {
      // console.log(this.companyId)
      this.formData = await getCompanyInfo(this.companyId)
    },
    async getRoleList() {
      // 获取员工信息
      const { total, rows } = await getRoleList(this.page)
      this.page.total = total
      this.currentList = rows
    },
    // 切换页
    currentChange(newPage) {
      this.page.page = newPage
      this.getRoleList()
    },
    // 删除员工信息
    async deleteRole(id) {
      // 是否删除
      await this.$confirm('是否删除')
      // 发送请求
      await delRole(id)
      // 提示删除成功
      this.$message.success('删除成功')
      // 刷新页面
      this.getRoleList()
    },

    // 编辑员工弹窗
    async editRole(id) {
      // 回显数据
      this.roleForm = await getRoleDetail(id)
      // 打开弹窗
      this.showDialog = true
    },
    // 确认
    async btnOK() {
      // 点击确认
      // 校验表单
      this.$refs.roleForm.validate()
      // 判断是否有id 有的话就是编辑 否则是新增
      if (this.roleForm.id) {
      // 发送编辑请求
        await updateRole(this.roleForm)
      } else {
        // 新增
        await addRole(this.roleForm)
      }
      // console.log(this.roleForm)

      // 提示用户
      this.$message.success('操作成功')
      // 更新页面
      this.getRoleList()
      // 关闭弹窗
      this.showDialog = false
    },
    // 页面弹窗 取消
    btnCancel() {
      // 点击取消清空表单验证
      this.$refs.roleForm.resetFields()
      // 清空数据信息
      this.roleForm = {}
      // 关闭弹窗
      this.showDialog = false
    },

    // 权限弹窗 确认
    async btnPermOK() {
      // 发送请求
      await assignPerm({
        id: this.roleId,
        permIds: this.$refs.permTree.getCheckedKeys()
      })
      // 提示用户
      this.$message.success('操作成功')
      // 关闭弹窗
      this.btnPermCancel()
    },
    // 权限弹窗 取消
    btnPermCancel() {
      // 设置列表树 的勾选为空 防止闪烁
      this.$refs.permTree.setCheckedKeys([])
      // 点击取消关闭弹窗
      this.showPermDialog = false
    },
    // 分配权限
    async  assignPerm(id) {
      // 保存id 编辑确认做准备
      this.roleId = id
      // 全部权限
      this.permList = listTotreeData(await getPermissionList(), '0')
      // 当前角色权限
      const { permIds } = await getRoleDetail(id)
      // 显示弹窗
      this.showPermDialog = true

      // 等待上面的内容都渲染完毕之后
      // 将拿回来的当前角色权限数据设置到 属性选中值当中
      this.$nextTick(() => {
        this.$refs.permTree.setCheckedKeys(permIds)
      })
    }
  }
}
</script>

<style>

</style>
